package com.haoxi.gateway.smgpmsg;

import com.haoxi.gateway.util.TypeConvert;



public class SMGPMtRouteUpdateMessage extends SMGPMessage {

    private StringBuffer strBuf;

    public SMGPMtRouteUpdateMessage(int updateType, int routeId,
            String srcGatewayId, String srcGatewayIP, int srcGatewayPort,
            String startTermId, String endTermId, String areaCode)
            throws IllegalArgumentException {
        if (updateType < 0 || updateType > 255)
            throw new IllegalArgumentException(
                    "The parameter of MtRouteUpdate message is wrong:UpdateType less than 0 or larger than 255.");

        if (srcGatewayId == null)
            throw new IllegalArgumentException(
                    "The parameter of MtRouteUpdate message is wrong:SrcGatewayId  value is null.");

        if (srcGatewayId.length() > 6)
            throw new IllegalArgumentException(
                    "The parameter of MtRouteUpdate message is wrong:SrcGatewayId length is larger than 6");

        if (srcGatewayIP == null)
            throw new IllegalArgumentException(
                    "The parameter of MtRouteUpdate message is wrong:SrcGatewayIP  value is null.");

        if (srcGatewayIP.length() > 15)
            throw new IllegalArgumentException(
                    "The parameter of MtRouteUpdate message is wrong:SrcGatewayIP length is larger than 15");

        if (startTermId == null)
            throw new IllegalArgumentException(
                    "The parameter of MtRouteUpdate message is wrong:StartTermId value is null.");

        if (startTermId.length() > 6)
            throw new IllegalArgumentException(
                    "The parameter of MtRouteUpdate message is wrong:StartTermId length is larger than 6");

        if (endTermId == null)
            throw new IllegalArgumentException(
                    "The parameter of MtRouteUpdate message is wrong:EndTermId value is null.");

        if (endTermId.length() > 6)
            throw new IllegalArgumentException(
                    "The parameter of MtRouteUpdate message is wrong:EndTermId length is larger than 6");

        if (areaCode == null)
            throw new IllegalArgumentException(
                    "The parameter of MtRouteUpdate message is wrong:AreaCode value is null.");

        if (areaCode.length() > 4) {
            throw new IllegalArgumentException(
                    "The parameter of MtRouteUpdate message is wrong:AreaCode length is larger than 4");

        } else {
            int len = 56;
            buf = new byte[len];
            TypeConvert.int2byte(len, buf, 0);
            TypeConvert.int2byte(SMGPCommon.SMGP_MT_ROUTE_UPDATE, buf, 4);
            buf[12] = (byte) updateType;
            TypeConvert.int2byte(routeId, buf, 13);
            System.arraycopy(srcGatewayId.getBytes(), 0, buf, 17, srcGatewayId
                    .length());
            System.arraycopy(srcGatewayIP.getBytes(), 0, buf, 23, srcGatewayIP
                    .length());
            TypeConvert.short2byte(srcGatewayPort, buf, 38);
            System.arraycopy(startTermId.getBytes(), 0, buf, 40, startTermId
                    .length());
            System.arraycopy(endTermId.getBytes(), 0, buf, 46, endTermId
                    .length());
            System
                    .arraycopy(areaCode.getBytes(), 0, buf, 52, areaCode
                            .length());
            strBuf = new StringBuffer(300);
            strBuf.append(",updateType=".concat(String.valueOf(String
                    .valueOf(updateType))));
            strBuf.append(",RouteID=".concat(String.valueOf(String
                    .valueOf(routeId))));
            strBuf.append(",SrcGatewayID=".concat(String.valueOf(String
                    .valueOf(srcGatewayId))));
            strBuf.append(",SrcGatewayIP=".concat(String.valueOf(String
                    .valueOf(srcGatewayIP))));
            strBuf.append(",SrcGatewayPort=".concat(String.valueOf(String
                    .valueOf(srcGatewayPort))));
            strBuf.append(",StartTermId=".concat(String.valueOf(String
                    .valueOf(startTermId))));
            strBuf.append(",EndTermId=".concat(String.valueOf(String
                    .valueOf(endTermId))));
            strBuf.append(",AreaCode=".concat(String.valueOf(String
                    .valueOf(areaCode))));
            return;
        }
    }

    public String toString() {
        StringBuffer outStr = new StringBuffer(300);
        outStr.append("SMGPMtRouteUpdateMessage:");
        outStr.append("PacketLength=".concat(String.valueOf(String
                .valueOf(buf.length))));
        outStr.append(",RequestID=8");
        outStr.append("Sequence_Id=".concat(String.valueOf(String
                .valueOf(getSequenceId()))));
        if (strBuf != null)
            outStr.append(strBuf.toString());
        return outStr.toString();
    }

    public int getRequestId() {
        return SMGPCommon.SMGP_MT_ROUTE_UPDATE;
    }
}
